/**
 * Created by 白雨浓 on 2018/1/23 12:19.
 */

import electron from 'electron';
import path from 'path';
import fs from 'fs';
import ejs from 'ejs';

export default {
    mapper(self, name){

        const dataPath = (electron.app || electron.remote.app).getPath('userData');
        const filePath = path.join(dataPath, 'mapper.ejs');
        if (!fs.existsSync(filePath)) {
            self.$$msg.error('mapper模板文件缺失');
            return;
        }
        const tmpFile = fs.readFileSync(filePath, 'utf-8');

        let clsName = self.$str.toCamel(name);
        clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

        let paramName = self.$str.toCamel(name);
        paramName = paramName.substring(0, 1).toLowerCase() + paramName.substring(1);

        let methods = [];
        if (self.selects.indexOf('select') >= 0) {
            methods.push({
                type: `${clsName}Entity`,
                name: 'select',
                params: [{type: `${clsName}Entity`, name: 'entity'}]
            })
        }
        if (self.selects.indexOf('selectList') >= 0) {
            methods.push({
                type: `List<${clsName}Entity>`,
                name: 'selectList',
                params: [{type: `${clsName}Entity`, name: 'entity'}]
            })
        }
        if (self.selects.indexOf('selectPage&Total') >= 0) {
            methods.push({
                type: `List<${clsName}Entity>`,
                name: 'selectPage',
                params: [
                    {type: `${clsName}Entity`, name: 'entity'},
                    {type: 'int', name: 'start'},
                    {type: 'int', name: 'size'}
                ]
            });
            methods.push({
                type: `int`,
                name: 'selectPageTotal',
                params: [{type: `${clsName}Entity`, name: 'entity'}]
            })
        }
        if (self.selects.indexOf('selectByKeyword') >= 0) {
            methods.push({
                type: `List<${clsName}Entity>`,
                name: 'selectByKeyword',
                params: [
                    {type: 'String', name: 'keyword'},
                    {type: 'int', name: 'start'},
                    {type: 'int', name: 'size'}
                ]
            });
            methods.push({
                type: `int`,
                name: 'selectTotalByKeyword',
                params: [{type: 'String', name: 'keyword'}]
            })
        }
        if (self.selects.indexOf('insert') >= 0) {
            methods.push({
                type: 'boolean',
                name: 'insert',
                params: [{type: `${clsName}Entity`, name: 'entity'}]
            })
        }
        if (self.selects.indexOf('update') >= 0) {
            methods.push({
                type: 'boolean',
                name: 'update',
                params: [{type: `${clsName}Entity`, name: 'entity'}]
            })
        }

        let tmp = {
            packageName: self.config.packageName,
            author: self.config.author,
            date: self.$date.format(new Date(), 'yyyy/MM/dd hh:mm'),
            clsName: clsName,
            methods: methods
        };

        return [ejs.render(tmpFile, tmp), methods];
    }
}